package com.common.basic.util;

import java.io.Serializable;
import java.util.Collection;

public class PageDto implements Serializable{
	private static final long serialVersionUID = 4191941284880661481L;

	private int index = 1;
	
	private int rowsPerPage  = 10;
	
	private int total;	
	
	private Collection<?> data;

	/**
	 * 
	 */
	public PageDto(){}
	
	/**
	 * @param index
	 */
	public PageDto(int index){
		this.index = index;
	}
	
	/**
	 * @param index
	 * @param rowsPerPage
	 */
	public PageDto(int index, int rowsPerPage){
		this(index);
		this.rowsPerPage = rowsPerPage;
	}
	
	/**
	 * @param index
	 * @param rowsPerPage
	 * @param total
	 */
	public PageDto(int index, int rowsPerPage, int total){
		this(index, rowsPerPage);
		this.total = total;
	}
	
	/**
	 * @param index
	 * @param rowsPerPage
	 * @param total
	 * @param data
	 */
	public PageDto(int index, int rowsPerPage, int total, Collection<?> data){
		this(index, rowsPerPage, total);
		this.data = data;
	}
	
	/**
	 * @return the index
	 */
	public int getIndex() {
		return index;
	}

	/**
	 * @param index the index to set
	 */
	public void setIndex(int index) {
		this.index = index;
	}

	/**
	 * @return the rowsPerPage
	 */
	public int getRowsPerPage() {
		return rowsPerPage;
	}

	/**
	 * @param rowsPerPage the rowsPerPage to set
	 */
	public void setRowsPerPage(int rowsPerPage) {
		this.rowsPerPage = rowsPerPage;
	}

	/**
	 * @return the total
	 */
	public int getTotal() {
		return total;
	}

	/**
	 * @param total the total to set
	 */
	public void setTotal(int total) {
		this.total = total;
	}

	/**
	 * @return the data
	 */
	public Collection<?> getData() {
		return data;
	}

	/**
	 * @param data the data to set
	 */
	public void setData(Collection<?> data) {
		this.data = data;
	}

	/**
	 * @param totalRecords
	 */
	public void calculate(int totalRecords)
	{
		if(getRowsPerPage()<=0)
			setRowsPerPage(10);
		int pgIdx    = getIndex();	        
		int pgRows   = getRowsPerPage();
		int pgTotal  = 0;
		int pgRowGrp = totalRecords / pgRows;
		int pgRowMod = totalRecords % pgRows;        
        if (pgRowMod == 0)
            pgTotal = pgRowGrp;
        else
            pgTotal = pgRowGrp + 1;        
        if (pgTotal == 0) pgTotal = 1;
        if (pgIdx <= 0) pgIdx = 1;
        if (pgIdx > pgTotal) pgIdx = pgTotal;        
        setIndex(pgIdx);
        setRowsPerPage(pgRows);
        setTotal(pgTotal);
	}
}
